/*******************************************************************************
																				
	DESCRIPTION:  	This do file produces Table A2.
					
*******************************************************************************/

clear all
global id_code 108_3

* Import programs:
run "${code}/Output Generation/108_0_MainStatistics_Program.do"

* Import baseline model predictions:
local vars _
local model Full
local year 2006
		
use "${data}/003_MainWithEnsemblePred_`model'`vars'`year'.dta", clear
keep if !missing(p_emplAft6M_0M_In)

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_Base

* Merge with model without trainees:
merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_NoTraining_2006.dta", ///
	keepusing(p_emplAft6M_0M_In training_combined_6months) ///
	assert(2 3) keep(3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_NT

* Merge with model without recalls:
merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_NoRecalls_2006.dta", ///
	keepusing(p_emplAft6M_0M_In recalled) ///
	assert(2 3) keep(3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_NR

* Merge with linear model:
merge 1:1 LopNr_PersonNr InLnr ///
	using "${data}/116_Linear_Predictions_`model'`vars'`year'.dta" ///
	, assert(2 3) keep(3) nogen keepusing(p_emplAft6M_0M_In_Linear)

* Merge with AvOrs dummies:
merge 1:1 LopNr_PersonNr InLnr using "${data}/001_01_UnemploymentSpells_Save", ///
	keepusing(AvOrs) assert(2 3) keep(3) nogen

tab AvOrs

* Merge with ML Robustness models
merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_ML_Robustness_1020Split_2006.dta", ///
	keepusing(p_emplAft6M_0M_In) ///
	assert(2 3) keep(3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_1020

merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_ML_Robustness_2020Split_2006.dta", ///
	keepusing(p_emplAft6M_0M_In) ///
	assert(3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_2020

merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_ML_Robustness_1040Split_2006.dta", ///
	keepusing(p_emplAft6M_0M_In) ///
	assert(1 3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_1040

merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_2006_withSpline.dta", ///
	keepusing(p_emplAft6M_0M_In) ///
	assert(1 3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_spline	

merge 1:1 LopNr_PersonNr InLnr using "${data}/003_MainWithEnsemblePred_Full_2006_PositiveWeights.dta", ///
	keepusing(p_emplAft6M_0M_In) ///
	assert(1 3) nogen

rename p_emplAft6M_0M_In p_emplAft6M_0M_In_poswe

	
	
* Create table:
file open myfile using "${output}/${id_code}_MainStatistics_Table_`model'`vars'`year'_Robustness.tex", write replace

file write myfile "%\documentclass{article}" _newline ///
	"%\usepackage{booktabs}" _newline ///
	"%\usepackage[margin=1in]{geometry}" _newline ///
	"%\begin{document}" _newline ///
	"%\begin{table}[h] \centering" _newline ///
	"\footnotesize \begin{tabular}{l c c c c c c c c}" _newline ///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	///
	"Model & Sample & N & \(E(F_{0})\) & \(E(\hat{F}_{0})\) & \(Var(F_{0})\) & \(Var(\hat{F}_{0})\)  & \(Cov(\hat{F}_{0}, F_{0})\) & \(R^2(\hat{F}_{0}, F_{0})\) \\ \addlinespace[0.1cm]" _newline ///
	" \hline \addlinespace[1.5ex]" _newline

* Baseline model, full sample:
stats emplAft6M_0M_In p_emplAft6M_0M_In_Base
file write myfile ///
	"\multicolumn{3}{l}{\textbf{A. Baseline}} \\" _newline ///
	"\cline{1-3} \addlinespace[0.2cm]" _newline ///
	"Baseline & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.4cm]" _newline

* Baseline model, sample with no training:
stats emplAft6M_0M_In p_emplAft6M_0M_In_Base if !training_combined_6months
file write myfile ///
	"\multicolumn{3}{l}{\textbf{B. Robustness to ALMPs}} \\" _newline ///
	"\cline{1-3} \addlinespace[0.2cm]" _newline ///
	"Baseline & No ALMPs & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline ///

* No ALMPs model, full sample:
stats emplAft6M_0M_In p_emplAft6M_0M_In_NT
file write myfile ///
	"No ALMPs & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

* No ALMPs model, sample with no training:
stats emplAft6M_0M_In p_emplAft6M_0M_In_NT if !training_combined_6months
file write myfile ///
	"No ALMPs & No ALMPs & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.4cm]"

* Baseline model, no AvOrs 7-8:
stats emplAft6M_0M_In p_emplAft6M_0M_In_Base if !inlist(AvOrs, "7 ", "8 ")
file write myfile ///
	"\multicolumn{3}{l}{\textbf{C. Robustness to job finding definition}} \\" _newline ///
	"\cline{1-3} \addlinespace[0.2cm]" _newline ///
	"Baseline & No AvOrs 7-8 & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

* Baseline model, no AvOrs 5-8:
stats emplAft6M_0M_In p_emplAft6M_0M_In_Base if !inlist(AvOrs, "5 ", "6 ", "7 ", "8 ")
file write myfile ///
	"Baseline & No AvOrs 5-8 & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

* Baseline model, no AvOrs 3:
stats emplAft6M_0M_In p_emplAft6M_0M_In_Base if recalled == 0
file write myfile ///
	"Baseline & No recalls & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline	
	
* No recalls model, full sample:
stats emplAft6M_0M_In p_emplAft6M_0M_In_NR
file write myfile ///
	"No Recalls & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline	
	
* No recalls model, no AvOrs 3:
stats emplAft6M_0M_In p_emplAft6M_0M_In_NR if recalled == 0
file write myfile ///
	"No Recalls & No recalls & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.4cm]" _newline	
		
	
* Linear model, full sample:
stats emplAft6M_0M_In p_emplAft6M_0M_In_Linear
file write myfile ///
	"\multicolumn{3}{l}{\textbf{D. Robustness to functional form}} \\" _newline ///
	"\cline{1-3} \addlinespace[0.2cm]" _newline ///
	"Linear & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline
	
* Individual ML predictors:
stats emplAft6M_0M_In p_emplAft6M_0M_In_rf
file write myfile ///
	"R. Forest & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

stats emplAft6M_0M_In p_emplAft6M_0M_In_boost
file write myfile ///
	"B. Gradient & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

stats emplAft6M_0M_In p_emplAft6M_0M_In_lasso
file write myfile ///
	"LASSO & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.4cm]" _newline

	
stats emplAft6M_0M_In p_emplAft6M_0M_In_1020
file write myfile ///
	"\multicolumn{3}{l}{\textbf{E. Robustness to sample split}} \\" _newline ///
	"\cline{1-3} \addlinespace[0.2cm]" _newline ///
	"\textit{Baseline: 10-30-10} & & & & & & & & \\ \addlinespace[0.2cm]"_newline ///
	"\(\uparrow\) Weights: 10-20-20 & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

stats emplAft6M_0M_In p_emplAft6M_0M_In_2020
file write myfile ///
	"\(\uparrow\) Tuning: 20-20-10 & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline

stats emplAft6M_0M_In p_emplAft6M_0M_In_1040
file write myfile ///
	"\(\uparrow\) Training: 10-40-10 & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.4cm]" _newline
	
* Positive weights:
stats emplAft6M_0M_In p_emplAft6M_0M_In_poswe
file write myfile ///
	"\multicolumn{3}{l}{\textbf{F. Robustness to ensemble weights}} \\" _newline ///
	"\cline{1-3} \addlinespace[0.2cm]" _newline ///
	"Positive weights & All & `r(n)' & `r(mean_f)' & `r(mean_f_hat)' & `r(var_f)'  & `r(var_f_hat)' & `r(cov)' & `r(r2)' \\ \addlinespace[0.2cm]" _newline
		
	
* Finish table:
file write myfile ///
	"\addlinespace[0ex]" _newline ///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	"\end{tabular}" _newline ///
	"%\end{table}" _newline ///
	"%\end{document}"

file close myfile	

